﻿using System;
using System.Linq;
using System.Xml.Linq;

public class Program
{

    private static readonly XNamespace employeesOld = "http://www.deitel.com/employeesold";
    private static readonly XNamespace employeesNew = "http://www.deitel.com/employeesnew";

    private static void Main(string[] args)
    {
        XDocument newDocument = XDocument.Load("employeesNew.xml");
        XDocument oldDocument = XDocument.Load("employeesOld.xml");

        oldDocument = TransformDocument(oldDocument);

        SaveFinalDocument(newDocument, oldDocument);

        Console.WriteLine("Documents successfully combined.");
    }

    private static XDocument TransformDocument(XDocument document)
    {
        var newDocumentRoot = new XElement(employeesNew + "employeelist",
            from employee in document.Root.Elements()
            select TransformEmployee(employee));

        return new XDocument(newDocumentRoot);
    }

    private static XElement TransformEmployee(XElement employee)
    {
        XNamespace old = employeesOld;

        string firstName = employee.Element(old + "firstname").Value;
        string lastName = employee.Element(old + "lastname").Value;
        string salary = employee.Element(old + "salary").Value;


        return new XElement(employeesNew + "employee",
            new XAttribute("name", firstName + " " + lastName),
            new XAttribute("salary", salary));
    }

    private static void SaveFinalDocument(XDocument document1,
        XDocument document2)
    {
        var root = new XElement(employeesNew + "employeelist");

        root.Add(document1.Root.Elements());
        root.Add(document2.Root.Elements());

        root.Save("output.xml");
    }
}


